// pages/my/itinerary/scenicIntroduce/scenicIntroduce.js

import Toast from '@vant/weapp/toast/toast';


Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    titleList: ["景点介绍", "游玩攻略", "热门游记"],
    value: 4,
    isBuy: 0,
    scenicId: 0,
    userId: null,
    scenic: null,
    travelogueList: [],
    collectOrNot: 0, // 0未收藏  1已收藏
    strategyList: [], //获取当前景点对应的攻略
  },


  back() {
    wx.navigateBack();
  },

  // 喜欢
  changeLike() {
    console.log('collectOrNot', this.data.collectOrNot)
    if (this.data.collectOrNot != 1) {
      // 对接后端数据库发请求...点赞数改变
      this.setData({
        collectOrNot: 1
      })
    } else {
      // 对接后端数据库发请求...点赞数改变
      this.setData({
        collectOrNot: 0
      })
    }
  },
  // 跳转
  // onPlan: function (e) {
  //   console.log(e)
  //   if (e.currentTarget.dataset.index == 0) {
  //     this.setData({
  //       isBuy: 1,
  //     })
  //   } else {
  //     this.setData({
  //       isBuy: 0
  //     })
  //   }
  // },
  //加入旅行清单，调用旅行计划的添加接口
  onPlan: function (event) {
    wx.request({
      url: 'http://localhost:8080/scenic/itinerary',
      method: "POST",
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        scenicId: this.data.scenicId,
        userId: this.data.userId
      },
      success: resp => {
        this.setData({
          isBuy: 1
        })
        wx.showToast({
          title: '收藏成功',
          icon: 'none'
        });
      }
    })
  },
  //取消收藏
  onCollect: function (event) {
    wx.request({
      url: 'http://localhost:8080/scenic/itinerary',
      method: "DELETE",
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        scenicId: this.data.scenicId,
        userId: this.data.userId
      },
      success: resp => {
        this.setData({
          isBuy: 0
        })
      }
    })
  },


  // 半星
  onChange(event) {
    this.setData({
      value: event.detail,
    });
  },

  // 选择scroll-view中的标题事件
  chooseItem(e) {
    this.setData({
      current: e.target.dataset.index
    })
  },

  // swiper的change事件函数
  swiperChanged(e) {
    // console.log(e.detail.current)
    this.setData({
      current: e.detail.current
    })
  },
  //加载当前景点的数据
  getScenicDetails: function (e) {
    wx.request({
      url: 'http://localhost:8080/scenic/scenic/list',
      method: "GET",
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        id: this.data.scenicId
      },
      success: resp => {
        if (resp.data.code == 200) {
          this.setData({
            scenic: resp.data.rows[0]
          })
        }
      }
    })
  },

  isCollect: function () {
    console.log(getApp().globalData)
    wx.request({
      url: 'http://localhost:8080/scenic/itinerary/isCollect',
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        scenicId: this.data.scenicId,
        userId: this.data.userId
      },
      success: resp => {
        this.setData({
          isBuy: resp.data
        })
      }
    })
  },

  // 攻略详情     
  getStrategy() {
    wx.request({
      url: `http://localhost:8080/strategy/strategy/list`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        scenicId: this.data.scenicId
      },
      success: (resp) => {
        this.setData({
          strategyList: resp.data.rows
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      scenicId: options.scenicId,
      userId: getApp().globalData.loginfo.userId
    })
    this.getScenicDetails()
    this.isCollect()
    this.getStrategy()
    wx.request({
      url: 'http://localhost:8080/travelogue/travelogue/list',
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        console.log(resp.data)
        this.setData({
          travelogueList: resp.data.rows
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})